/*
 * @Author: zxh
 * @Email: 1271027008@qq.com
 * @Date: 2020-06-09 16:22:27
 * @Descripttion: 
 */ 
import { Component, OnInit } from '@angular/core';
import { FormGroup, FormBuilder, Validators, FormControl } from "@angular/forms";
import {  NzModalService } from 'ng-zorro-antd/modal';
import { NzMessageService } from 'ng-zorro-antd/message';
import { CenterManageService } from './center-manage.service'
import { Router } from "@angular/router";

@Component({
  selector: 'app-center-manage',
  templateUrl: './center-manage.component.html',
  styleUrls: ['./center-manage.component.scss']
})
export class CenterManageComponent implements OnInit {

  constructor(
    private message: NzMessageService,
    private modalService: NzModalService,
    private fb: FormBuilder,
    private centerService:CenterManageService,
    private router : Router
  ) { }

  passwordForm: FormGroup;

  ngOnInit() {

    this.passwordForm = this.fb.group({
      oldPass: [null, Validators.required],
      newPass: [null, Validators.required],
      confirmNewPass: [null, [this.confirmValidator]],
    })

  }
  validateConfirmPassword(): void {
    setTimeout(() => this.passwordForm.controls.confirmNewPass.updateValueAndValidity());
  }
  confirmValidator = (control: FormControl): { [s: string]: boolean } => {
    if (!control.value) {
      return { error: true, required: true };
    } else if (control.value !== this.passwordForm.controls.newPass.value) {
      return { confirmNewPass: true, error: true };
    }
    return {};
  }
  submitEditPassword() {
    var _dataJson = {
      oldPass:'',
      confirmNewPass:''
    }
    for (const i in this.passwordForm.controls) {
        this.passwordForm.controls[i].markAsDirty();
        this.passwordForm.controls[i].updateValueAndValidity();
        _dataJson[i] = this.passwordForm.controls[i].value
    }
    if (this.passwordForm.status === "VALID") {
        this.centerService.editPassword(_dataJson).subscribe(res=>{
            this.message.success('修改成功！请重新登录')
            this.router.navigate(['login']);
        })
    };
  }
  resetForm(){
    this.passwordForm.reset()
  }

}
